package com.triones.yjj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.triones.yjj.entity.MarkMemberRuleDO;
import com.triones.yjj.entity.vo.MarkMemberRuleVO;
import com.triones.yjj.mapper.MarkMemberRuleMapper;
import com.triones.yjj.service.MarkMemberRuleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 规则管理 service 业务实现
 *
 * @Author: triones
 */
@Service
@RequiredArgsConstructor
public class MarkMemberRuleServiceImpl implements MarkMemberRuleService {

    private final MarkMemberRuleMapper markMemberRuleMapper;

    @Override
    public MarkMemberRuleVO queryRule() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        LambdaQueryWrapper<MarkMemberRuleDO> wrapper = Wrappers.lambdaQuery(MarkMemberRuleDO.class);
        wrapper.eq(MarkMemberRuleDO::getUserId,loginUser.getUserId());
        return markMemberRuleMapper.selectVoOne(wrapper);
    }

    @Override
    public Boolean saveRule(Integer value) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        LambdaQueryWrapper<MarkMemberRuleDO> wrapper = Wrappers.lambdaQuery(MarkMemberRuleDO.class);
        wrapper.eq(MarkMemberRuleDO::getUserId,loginUser.getUserId());
        MarkMemberRuleDO memberRuleDO = markMemberRuleMapper.selectOne(wrapper);
        if (memberRuleDO == null) {
            memberRuleDO = new MarkMemberRuleDO();
            memberRuleDO.setUserId(loginUser.getUserId());
            memberRuleDO.setIsMarkMemberToOld(value);
            return markMemberRuleMapper.insert(memberRuleDO) > 0;
        } else {
            memberRuleDO.setIsMarkMemberToOld(value);
            return markMemberRuleMapper.updateById(memberRuleDO) > 0;
        }
    }
}
